package com.bbm.communitysystem.controller;

import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.core.oss.OSS;
import com.bbm.communitysystem.core.redis.PostRedis;
import com.bbm.communitysystem.domain.OssFile;
import com.bbm.communitysystem.service.ILikePostService;
import com.bbm.communitysystem.service.IPostService;
import com.bbm.communitysystem.service.IUserInfoService;
import com.bbm.communitysystem.service.impl.CommentServiceImpl;
import com.bbm.communitysystem.service.impl.OssFileServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "a国伟的学习接口")
@RestController
@RequestMapping("/Mr_chen/learn")
@RequiredArgsConstructor
public class LearnController {

    private final IPostService postService;

    private final IUserInfoService userInfoService;

    private final OssFileServiceImpl ossFileService;

    private final ILikePostService likePostService;

    private final CommentServiceImpl commentService;

    private final PostRedis postRedis;

    private final OSS oss;


    @Operation(summary = "上传图片")
    @PostMapping("/upload")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID"),
            @Parameter(name = "img", description = "图片")
    })
    public R LearnUploadImg(@RequestParam("userId") Integer userId, @RequestParam("img") MultipartFile img) {

        if (img.isEmpty()) {
            return R.ok("请上传文件");
        }

        List<OssFile> learn = oss.saveFile("learn", userId.toString(), img);

        if (learn.isEmpty()) {
            return R.error("上传失败");
        }



        return R.ok("upload your file success！", learn.get(0).getClientUrl());
    }



    @Operation(summary = "上传视频", description = "当chunkHash为完成时，后端开始合并文件")
    @PostMapping("/learn/upload/big")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID"),
            @Parameter(name = "file", description = "文件块"),
            @Parameter(name = "fileHash", description = "视频hash"),
            @Parameter(name = "chunkHash", description = "文件块hash"),
            @Parameter(name = "index", description = "文件块索引"),
            @Parameter(name = "originFileName", description = "视频源名称"),
    })
    public R LearnUploadVideo(@RequestParam("userId") Integer userId, @RequestParam("file") MultipartFile img,
                              @RequestParam("fileHash") String fileHash, @RequestParam("chunkHash") String chunkHash,
                              @RequestParam("index") Integer index, @RequestParam("originFileName") String originFileName) {


        if (chunkHash.equals("完结")) {

            OssFile ossFile = oss.saveBigFile("learn", userId.toString(), fileHash, originFileName);
            return ossFile != null ? R.ok("合并成功") : R.error("合并失败");
        } else {

            Boolean b = oss.saveCheckFile(img, fileHash, userId.toString(), index.toString());

            return b ? R.ok("上传成功") : R.error("上传失败");
        }


    }


}
